from django.shortcuts import render
from mylearn.models import *
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from rest_framework.response import Response
from rest_framework.views import APIView
from mylearn.mylearnser import *
# 导入redis
import redis
import time

# 定义ip端口
host = "localhost"
port = 6379
# 建立连接
r = redis.Redis(host=host, port=port)


# 打卡视图
class Punch(APIView):
    def get(self, request):
        name = request.GET.get('name')
        user = Peoples.objects.filter(name=name).first()
        print(name)

        if user.is_work == 0:
            # 将时间戳存入redis
            r.set(user.name, time.time())
            # 将状态改为上班
            user.is_work = 1
            user.save()
            return Response({'message':'打卡成功'})
        else:
            start_secs = float(r.get(user.name))
            end_secs = time.time()
            print('en_secs', time.localtime(end_secs))
            # 计算上班时间
            hours = time.localtime(end_secs).tm_hour - time.localtime(start_secs).tm_hour
            print('我要打印时间')
            print(hours)

            # 判断所处部门
            print(3333333333)
            dep = Department.objects.get(id=user.did)
            print(2222222222)
            if dep.sid == 2:
                fen = hours * 0.1
            else:
                fen = hours * 0.5
            # #反序列化打开记录
            data = {
                'name': user.name,
                'push_time': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start_secs)),
                'work_time': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(start_secs)),
                'off_tiem': time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(end_secs)),
                'fen': "+%s" % fen
            }

            card = CardSer(data=data)
            if card.is_valid():
                card.save()
                return Response({'message':"恭喜下班"})

            user.is_work = 0
            user.save()
            r.delete(user.name)
        return Response({'code': 200, 'msg': '成功'})


# 展示打卡的人员
class ShowUser(APIView):
    def get(self, request):
        user = Peoples.objects.all()
        ser = PeoplesSer(user, many=True).data
        return Response(ser)


# 升级操作
class Upgrade(APIView):
    def get(self, request):
        id = request.GET.get('id')
        print(id)
        user = Peoples.objects.get(id=id)

        # 根据用户查询 所属部门
        dep = Department.objects.filter(sid=user.did).first()
        # print(dep.sid.id)
        try:
            # 如果所属部门有上级部门
            print(111)
            if dep.sid and user.upgrade == 2:
                # 替换用户sid 为上级部门
                user.did = 1
                user.upgrade = 0
                print(333)
                user.save()
                print('我是if里面的')
        except Exception as e:
            print(e)
        return Response({'code': 200, 'msg': '升级成功'})
#查询接口
class Search(APIView):
    def get(self,request):
        name=request.GET.get('text')
        print(name)
        card=Card.objects.filter(name=name).order_by('-push_time')
        cardser=CardSer(card,many=True)
        return Response({'data':cardser.data})
